import com.sun.deploy.net.MessageHeader;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-03-31
 * Time: 10:03
 */
public class Test {
    public static int find(int[] array, int val) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == val) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 5, 7, 8};
        int ret = find(array, 8);
        System.out.println(ret);
    }

    public static void main9(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int[] array2 = array.clone();
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main8(String[] args) {
        //判断数组内容是否相等
        int[] array1 = {1, 2, 3, 4, 56};
        int[] array2 = {1, 2, 3, 4, 5};
        System.out.println(Arrays.equals(array1, array2));

        // fill 使数组的值发生变化
        int[] array3 = new int[10];
        Arrays.fill(array3, 1, 5, -1);// from end  val
        System.out.println(Arrays.toString(array3));
    }

    public static void main7(String[] args) {
        //拷贝其中的一部分
        int[] array = {1, 2, 3, 4, 5, 6, 8, 9};
        int[] tmp = Arrays.copyOfRange(array, 2, 6);// [ ) 左闭右开的区间
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(tmp));
    }

    public static void main6(String[] args) {
        //copyOf可以当扩容来使用
        int[] array = {1, 2, 3, 4, 5};
        //int[] ret = Arrays.copyOf(array,array.length/2);
        int[] ret = Arrays.copyOf(array, array.length * 2);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));

    }

    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4};
        int[] tmp = new int[array.length];
        //arraycopy是copyOf的底层  在System里面
        System.arraycopy(array, 0, tmp, 0, array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(tmp));

    }

    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        //使用方法拷贝
        int[] res = Arrays.copyOf(array, array.length);//src length
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(res));
    }

    public static void main3(String[] args) {
        //深拷贝
        int[] array1 = {1, 2, 3, 4, 5, 6, 7};
        int[] array2 = new int[array1.length];
        for (int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));

        System.out.println("======================");
        array2[0] = 999;
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));

    }

    public static void main2(String[] args) {
        //这种不是拷贝，是两个变量指向同一个对象
        int[] array1 = {1, 2, 3, 4};
        System.out.println(Arrays.toString(array1));

        int[] array2 = array1;
        System.out.println(Arrays.toString(array2));

    }

    public static String mytoString(int[] array) {
        if (array == null) {
            return "null";
        }

        String tmp = "[";
        for (int i = 0; i < array.length; i++) {
            tmp += array[i];
            if (i != array.length - 1) {
                tmp += ",";
            }
        }
        tmp += "]";
        return tmp;
    }

    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        String str = mytoString(array);
        System.out.println(str);
    }
}
